package com.shwread.android.newui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shwread.android.newui.bean.TopContentBean;
import com.shwread.android.qysw10000229.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import logic.bean.RegionContentItemBean;

/**
 * Created by lex on 2017/7/6.
 */

public class EnterpriseAdapter extends RecyclerView.Adapter<EnterpriseAdapter.EnterpriseListHolder> implements View.OnClickListener {

    private Context context;
    private List<RegionContentItemBean> list;

    private OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener){
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    public EnterpriseAdapter(Context context, List<RegionContentItemBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public EnterpriseAdapter.EnterpriseListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(
                context).inflate(R.layout.item_enterprise_list, parent,
                false);
        EnterpriseListHolder holder = new EnterpriseListHolder(view);
        view.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(EnterpriseAdapter.EnterpriseListHolder holder, int position) {
        String jsonStr = list.get(position).getDataJson();
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            holder.tvItemTitle.setText(jsonObject.getString("title"));
            holder.itemView.setTag(list.get(position));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View view) {
        onRecyclerViewItemClickListener.onItemClick(view,view.getTag());
    }

    class EnterpriseListHolder extends RecyclerView.ViewHolder {

        private TextView tvItemTitle;

        public EnterpriseListHolder(View view) {
            super(view);
            tvItemTitle = (TextView) view.findViewById(R.id.tvItemTitle);
        }
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, Object data);
    }
}
